package com.oocl.interaction;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.oocl.client.ClientInfoList;
import com.oocl.global.GlobalVar;
import com.oocl.server.po.ClientInfo;

public class Browser implements Runnable {

	private static Socket socket;

	public Browser(Socket socket) {
		super();
		this.socket = socket;
	}

	public void brow() throws IOException {
		OutputStream out = socket.getOutputStream();

		writeHTTPProtocal(out);
		readHtmlToHttp(out);
		
		int buttonRequest = getButtonRequest(socket);
		responseButtons(buttonRequest, out);
		
		out.close();
		socket.close();
		
	}

	public static void responseButtons (int buttonRequest, OutputStream out) throws IOException {
		if (buttonRequest == 1) {
			boolean isOpen = openTcpServer();
			out.write("<br>TCP server start!</br>".getBytes());
		} 
		if (buttonRequest == 3) {
			if (GlobalVar.isOpenTcpServer) {
				out.write("<br>Online Clients Information</br>".getBytes());
				out.write("<br>----------------- Client IP ----------------- Client Start Time----------------</br>"
						.getBytes());
				writeClientInfoList(out);
			}
		}
		if (buttonRequest == 2){
			out.write("<br>TCP server stop!</br>".getBytes());
			boolean isClose = closeTcpServer();
		}
	}
	
	public static void writeClientInfoList(OutputStream out) throws IOException {
		List<ClientInfo> clientInfos = new ArrayList<ClientInfo>();
		ClientInfoList cInfoList = new ClientInfoList(socket);
		clientInfos = cInfoList.getClinetsInfo();
		
		if (!clientInfos.isEmpty()) {
			for (int i = 0; i < clientInfos.size(); i++) {
				out.write("<br>".getBytes());
				ClientInfo clientInfo = new ClientInfo();
				out.write(clientInfo.objectToString(clientInfos.get(i)).getBytes());
				out.write("</br>".getBytes());
			}
		}
		else {
			out.write("<br>There is no online client.</br>".getBytes());
		}
	}
	
	public static void readHtmlToHttp(OutputStream out) throws IOException {
		DataInputStream in = new DataInputStream(new FileInputStream("state.html"));
		int m = 0;
		while ((m = in.read()) != -1) {
			out.write(m);
		}
		in.close();
	}
	
	public static void writeHTTPProtocal(OutputStream out) throws IOException {
		out.write("HTTP/1.0 200 OK\r\n".getBytes());
		out.write("Content-Type:text/html;charset=utf-8\r\n".getBytes());
		out.write("\r\n".getBytes());
	}
	
	public static int getButtonRequest(Socket socket) throws IOException {
		InputStream inputStream = socket.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		String line = br.readLine();
		if (line.contains("stop")) {
			return 2;
		}
		if (line.contains("start")) {
			return 1;
		}
		if (line.contains("show")) {
			return 3;
		}
		
		return 0;
	}

	public static boolean openTcpServer() {
		GlobalVar.isOpenTcpServer = true;
		return true;
	}

	public static boolean closeTcpServer() {
		GlobalVar.isOpenTcpServer = false;
		return true;
	}

	@Override
	public void run() {
		try {
			System.out.println("browser!!!!!!!!!!!!!!");
			brow();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
